/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_z_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) != 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d != mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_cmp(v->v.mpz, r0.v.mpz) != 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = !mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_z_lt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 < mpz_sgn(r0.v.mpz);
	    break;
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) > 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d < mpz_get_d(r0.v.mpz);
	    break;
	case t_shz:	case t_mpz:
	    r0.v.i = mpz_cmp(v->v.mpz, r0.v.mpz) < 0;
	    break;
	case t_shq:	case t_mpq:
	    mpq_set_z(thr_q0, r0.v.mpz);
	    r0.v.i = mpq_cmp(v->v.mpq, thr_q0) < 0;
	    break;
	case t_shr:	case t_mpr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = mpfr_less_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_z_le(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 <= mpz_sgn(r0.v.mpz);
	    break;
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) >= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d <= mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_cmp(v->v.mpz, r0.v.mpz) <= 0;
	    break;
	case t_mpq:		case t_shq:
	    mpq_set_z(thr_q0, r0.v.mpz);
	    r0.v.i = mpq_cmp(v->v.mpq, thr_q0) <= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_z_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) == 0;
	    break;
	case t_float:
	    r0.v.i = mpz_get_d(r0.v.mpz) == v->v.d;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_cmp(r0.v.mpz, v->v.mpz) == 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_z_ge(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 >= mpz_sgn(r0.v.mpz);
	    break;
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) <= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d >= mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_cmp(v->v.mpz, r0.v.mpz) >= 0;
	    break;
	case t_mpq:		case t_shq:
	    mpq_set_z(thr_q0, r0.v.mpz);
	    r0.v.i = mpq_cmp(v->v.mpq, thr_q0) >= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_z_gt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 > mpz_sgn(r0.v.mpz);
	    break;
	case t_int:
	    r0.v.i = empz_cmp_i(r0.v.mpz, v->v.i) < 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d > mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_cmp(v->v.mpz, r0.v.mpz) > 0;
	    break;
	case t_mpq:		case t_shq:
	    mpq_set_z(thr_q0, r0.v.mpz);
	    r0.v.i = mpq_cmp(v->v.mpq, thr_q0) > 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_rr0, r0.v.mpz, thr_rnd);
	    r0.v.i = mpfr_greater_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_z_and(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (likely(ez_and_i_z(thr_zr, v->v.i, r0.v.mpz))) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpz:		case t_shz:
	    if (ez_and_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_and_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_or(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    ez_or_i_z(thr_zr, v->v.i, r0.v.mpz);
	    chk_rz();
	    break;
	case t_mpz:		case t_shz:
	    ez_or_z_z(thr_zr, v->v.mpz, r0.v.mpz);
	    chk_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_or_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_xor(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    if (ez_xor_i_z(thr_zr, v->v.i, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpz:		case t_shz:
	    if (ez_xor_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_xor_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    if (ez_add_i_z(thr_zr, v->v.i, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d + mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (ez_add_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_add_q_z(thr_qr, v->v.mpq, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_add_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd + mpz_get_d(r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_add_qq_z(thr_qq, v->v.cqq, r0.v.mpz);
	    set_rqq();
	    break;
	case t_shc:	case t_mpc:
	    ecc_add_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    mpz_neg(thr_zr, r0.v.mpz);
	    if (empz_fit_i(thr_zr)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_int:
	    if (ez_sub_i_z(thr_zr, v->v.i, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d - mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (ez_sub_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_sub_q_z(thr_qr, v->v.mpq, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_sub_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd - mpz_get_d(r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_sub_qq_z(thr_qq, v->v.cqq, r0.v.mpz);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (ez_mul_i_z(thr_zr, v->v.i, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d * mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (ez_mul_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_mul_q_z(thr_qr, v->v.mpq, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_mul_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd * mpz_get_d(r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_mul_qq_z(thr_qq, v->v.cqq, r0.v.mpz);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    switch (eq_div_i_z(thr_qr, v->v.i, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d / mpz_get_d(r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_div_z_z(thr_qr, v->v.mpz, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_div_q_z(thr_qr, v->v.mpq, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd / mpz_get_d(r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_div_qq_z(thr_qq, v->v.cqq, r0.v.mpz);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (ez_trunc_i_z(thr_zr, v->v.i, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_float:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_d(thr_zr, v->v.d /
				     mpz_get_d(r0.v.mpz))) == MININT)
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    if (ez_trunc_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    if (ez_trunc_q_z(thr_zr, v->v.mpq, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_shr:	case t_mpr:
	    if (ez_trunc_r_z(thr_zr, v->v.mpr, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_trunc_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (likely(ez_rem_i_z(thr_zr, v->v.i, r0.v.mpz))) {
		/* FIXME can this not be true? (issue with MININT and signs?) */
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = fmod(v->v.d, mpz_get_d(r0.v.mpz));
	    break;
	case t_mpz:		case t_shz:
	    if (ez_rem_z_z(thr_zr, v->v.mpz, r0.v.mpz)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_rem_q_z(thr_qr, v->v.mpq, r0.v.mpz)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case 0:
		    set_rq();
		    break;
		default:
		    chk_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_rem_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_d_z(0.0, r0.v.mpz);
	    }
	    else {
		er_atan2_i_z(thr_rr, 0, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_i_z(v->v.i, r0.v.mpz);
	    }
	    else {
		er_atan2_i_z(thr_rr, v->v.i, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_atan2_d_z(v->v.d, r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_z_z(v->v.mpz, r0.v.mpz);
	    }
	    else {
		er_atan2_z_z(thr_rr, v->v.mpz, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_q_z(v->v.mpq, r0.v.mpz);
	    }
	    else {
		er_atan2_q_z(thr_rr, v->v.mpq, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_dd_z(v->v.cdd, r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_qq_z(v->v.cqq, r0.v.mpz);
		chk_rdd();
	    }
	    else {
		ecc_atan2_qq_z(thr_c, v->v.cqq, r0.v.mpz);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_d_z(0.0, r0.v.mpz);
	    }
	    else {
		er_pow_i_z(thr_rr, 0, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_i_z(v->v.i, r0.v.mpz);
	    }
	    else {
		er_pow_i_z(thr_rr, v->v.i, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_pow_d_z(v->v.d, r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_z_z(v->v.mpz, r0.v.mpz);
	    }
	    else {
		er_pow_z_z(thr_rr, v->v.mpz, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_q_z(v->v.mpq, r0.v.mpz);
	    }
	    else {
		er_pow_q_z(thr_rr, v->v.mpq, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_pow_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_pow_dd_z(v->v.cdd, r0.v.mpz);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_qq_z(v->v.cqq, r0.v.mpz);
		chk_rdd();
	    }
	    else {
		ecc_pow_qq_z(thr_c, v->v.cqq, r0.v.mpz);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_z(thr_c, v->v.mpc, r0.v.mpz);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_z(r0.v.mpz);
	    }
	    else {
		er_abs_z(thr_rr, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_i_z(v->v.i, r0.v.mpz);
	    }
	    else {
		er_hypot_i_z(thr_rr, v->v.i, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_d_z(v->v.d, r0.v.mpz);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_z_z(v->v.mpz, r0.v.mpz);
	    }
	    else {
		er_hypot_z_z(thr_rr, v->v.mpz, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_q_z(v->v.mpq, r0.v.mpz);
	    }
	    else {
		er_hypot_q_z(thr_rr, v->v.mpq, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_z(thr_rr, v->v.mpr, r0.v.mpz);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_dd_z(v->v.cdd, r0.v.mpz);
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = ed_hypot_qq_z(v->v.cqq, r0.v.mpz);
		chk_rdd();
	    }
	    else {
		er_hypot_qq_z(thr_rr, v->v.cqq, r0.v.mpz);
		set_rr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_z(thr_rr, v->v.mpc, r0.v.mpz);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_mpz(v->v.o, r0.v.mpz, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_z_complex(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    mpq_set_z(thr_qi, r0.v.mpz);
	    mpq_set_ui(thr_qr, 0, 1);
	    set_rqq();
	    break;
	case t_int:
	    mpq_set_z(thr_qi, r0.v.mpz);
	    empq_set_i(thr_qr, v->v.i);
	    set_rqq();
	    break;
	case t_float:
	    r0.t = t_cdd;
	    imag(r0.v.cdd) = mpz_get_d(r0.v.mpz);
	    real(r0.v.cdd) = v->v.d;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    mpq_set_z(thr_qi, r0.v.mpz);
	    mpq_set_z(thr_qr, v->v.mpz);
	    set_rqq();
	    break;
	case t_mpq:		case t_shq:
	    mpq_set_z(thr_qi, r0.v.mpz);
	    mpq_set(thr_qr, v->v.mpq);
	    set_rqq();
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_z(thr_ri, r0.v.mpz, thr_rnd);
	    mpfr_set(thr_rr, v->v.mpr, thr_rnd);
	    set_chk_rrr();
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_z_inc(void)
{
    if (r0.t == t_rgz) {
	mpz_add_ui(thr_zr, thr_zr, 1);
	if (empz_fit_i(thr_zr)) {
	    r0.t = t_int;
	    r0.v.i = empz_get_i(thr_zr);
	}
    }
    else {
	mpz_add_ui(thr_zr, r0.v.mpz, 1);
	if (empz_fit_i(thr_zr)) {
	    r0.t = t_int;
	    r0.v.i = empz_get_i(thr_zr);
	}
	else
	    set_rz();
    }
}

void
evm_z_dec(void)
{
    if (r0.t == t_rgz) {
	mpz_sub_ui(thr_zr, thr_zr, 1);
	if (empz_fit_i(thr_zr)) {
	    r0.t = t_int;
	    r0.v.i = empz_get_i(thr_zr);
	}
    }
    else {
	mpz_sub_ui(thr_zr, r0.v.mpz, 1);
	if (empz_fit_i(thr_zr)) {
	    r0.t = t_int;
	    r0.v.i = empz_get_i(thr_zr);
	}
	else
	    set_rz();
    }
}
